package cn.janescott.framework.druid;

import cn.janescott.framework.druid.annotation.EnableDruidDataSourceConfiguration;
import cn.janescott.framework.druid.properties.DruidProperties;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import java.sql.SQLException;

/**
 * alibaba druid数据源
 *
 * @author scott
 * @date 2018/7/4
 */
@Configuration
@EnableConfigurationProperties(value = DruidProperties.class)
@ConditionalOnBean(annotation = EnableDruidDataSourceConfiguration.class)
public class DruidConfiguration {
    /**
     * 配置
     */
    private DruidProperties properties;

    /**
     * 数据源配置
     */
    @Bean(destroyMethod = "close", initMethod = "init")
    @Primary
    public DruidDataSource druidDatasource() {
        DruidDataSource datasource = new DruidDataSource();
        datasource.setUrl(properties.getUrl());
        datasource.setUsername(properties.getUsername());
        datasource.setPassword(properties.getPassword());
        datasource.setDriverClassName(properties.getDriverClassName());

        //configuration
        datasource.setInitialSize(properties.getInitialSize());
        datasource.setMinIdle(properties.getMinIdle());
        datasource.setMaxActive(properties.getMaxActive());
        datasource.setMaxWait(properties.getMaxWait());
        datasource.setTimeBetweenEvictionRunsMillis(properties.getTimeBetweenEvictionRunsMillis());
        datasource.setMinEvictableIdleTimeMillis(properties.getMinEvictableIdleTimeMillis());
        datasource.setValidationQuery(properties.getValidationQuery());
        datasource.setTestWhileIdle(properties.isTestWhileIdle());
        datasource.setTestOnBorrow(properties.isTestOnBorrow());
        datasource.setTestOnReturn(properties.isTestOnReturn());
        datasource.setPoolPreparedStatements(properties.isPoolPreparedStatements());
        datasource.setMaxPoolPreparedStatementPerConnectionSize(properties.getMaxPoolPreparedStatementPerConnectionSize());
        // 是否开启监控
        if (properties.isEnableFilter()) {
            datasource.setUseGlobalDataSourceStat(properties.isUseGlobalDataSourceStat());
            try {
                datasource.setFilters(properties.getFilters());
            } catch (SQLException | UnsupportedOperationException e) {
                System.err.println("druid configuration initialization filter: " + e);
            }
            datasource.setConnectionProperties(properties.getConnectionProperties());
        }
        return datasource;
    }

    @Autowired
    public void setDruidProperties(DruidProperties druidProperties) {
        this.properties = druidProperties;
    }
}
